package com.heny.demo.service.impl;

import com.heny.demo.entity.TurnPoint;
import com.heny.demo.mapper.TurnPointMapper;
import com.heny.demo.service.ITurnPointService;
import com.heny.demo.strategy.file.FileImportStrategy;
import com.heny.demo.strategy.file.FileImportStrategyFactory;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.io.InputStream;
import java.util.List;

/**
 * 地信坐标文件转换service业务实现层
 *
 * @author 邢益萌
 * {@code @date} 2025/9/11 15:11
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class TurnPointServiceImpl implements ITurnPointService {

    private final TurnPointMapper turnPointMapper;


    @Override
    public Boolean uploadTurnPoint(String fileType, InputStream inputStream) {
        //获取对应文件的解析方式
        FileImportStrategy strategy = FileImportStrategyFactory.getStrategy(fileType);
        //转换坐标
        List<TurnPoint> turnPoints = strategy.importFile(inputStream);
        if (CollectionUtils.isEmpty(turnPoints)) {
            return true;
        }

        int count;
        try {
            //批量插入
            count = turnPointMapper.insertBatch(turnPoints);
        } catch (Exception e) {
            log.error("坐标数据插入失败：{}", e.getMessage());
            return false;
        }
        return count > 0;
    }
}
